package 算法.线性表;

import jdk.nashorn.internal.objects.annotations.Where;

import java.util.Arrays;

public class 快排 {
    public static Integer[] array;

    public static void main(String[] args) {
        array = new Integer[]{4, 5, 3, 1, 8, 2, 9, 7, 0, 6, 11, 10};
        int i = 0, j = array.length - 1;
        Integer min = QuickSort(array, i, j);
        while (min > 2) {
            min = QuickSort(array, i, min - 1);
            min = QuickSort(array, min + 1, j);

        }
        System.out.println(Arrays.toString(array));
    }

    private static Integer QuickSort(Integer[] array, int i, int j) {
        int temp = array[i];
        while (i < j) {
            while (array[j] > temp && j > i) {
                j--;
            }
            while (array[i] < temp && j > i) {
                i++;
            }
            Swap(array, i, j);
        }
        array[i] = temp;
        return i;
    }

    // 交换
    private static void Swap(Integer[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

}
